package apibasej.db.to;

import java.io.Serializable;
import java.util.ArrayList;

import apibasej.xml.NodeXML;

@SuppressWarnings("serial")
@NodeXML
public class StatementValues implements Serializable{

	@NodeXML private String sql;
	@NodeXML private ArrayList<StatementParam<?>> params;
	
	// usado somente para o select ... para saber o tipo de resposta para montar getValResultSet do CursorSelect ...
	@NodeXML private ArrayList<Class<?>> typesColsSelect;
	
	@NodeXML(asAttribute=true) private Integer qtdAffectedRowsToValidate;
	@NodeXML(asAttribute=true) private Integer maxAffectedRowsToValidate;
	
	public String getSql() {
		return sql;
	}
	public ArrayList<StatementParam<?>> getParams() {
		if(params==null) params = new ArrayList<StatementParam<?>>();
		return params;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public void setParams(ArrayList<StatementParam<?>> params) {
		this.params = params;
	}
	public ArrayList<Class<?>> getTypesColsSelect() {
		return typesColsSelect;
	}
	public void setTypesColsSelect(ArrayList<Class<?>> typesColsSelect) {
		this.typesColsSelect = typesColsSelect;
	}
	
	public Integer getQtdAffectedRowsToValidate() {
		return qtdAffectedRowsToValidate;
	}
	public void setQtdAffectedRowsToValidate(Integer qtdAffectedRowsToValidate) {
		this.qtdAffectedRowsToValidate = qtdAffectedRowsToValidate;
	}
	public Integer getMaxAffectedRowsToValidate() {
		return maxAffectedRowsToValidate;
	}
	public void setMaxAffectedRowsToValidate(Integer maxAffectedRowsToValidate) {
		this.maxAffectedRowsToValidate = maxAffectedRowsToValidate;
	}
	
	public boolean isQuery(){
		return typesColsSelect!=null;
	}
	
	public String getSqlInfo(){
		StringBuilder sqlInfo = new StringBuilder();
		if(sql!=null) {
			sqlInfo.append(sql);
			sqlInfo.append("\r\n");
			for(StatementParam<?> e : getParams()) sqlInfo.append(e.getStrInfo());
		}

		return sqlInfo.toString();
	}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((params == null) ? 0 : params.hashCode());
		result = prime * result + ((qtdAffectedRowsToValidate == null) ? 0 : qtdAffectedRowsToValidate.hashCode());
		result = prime * result + ((sql == null) ? 0 : sql.hashCode());
		result = prime * result + ((typesColsSelect == null) ? 0 : typesColsSelect.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StatementValues other = (StatementValues) obj;
		if (params == null) {
			if (other.params != null)
				return false;
		} else if (!params.equals(other.params))
			return false;
		if (qtdAffectedRowsToValidate == null) {
			if (other.qtdAffectedRowsToValidate != null)
				return false;
		} else if (!qtdAffectedRowsToValidate.equals(other.qtdAffectedRowsToValidate))
			return false;
		if (sql == null) {
			if (other.sql != null)
				return false;
		} else if (!sql.equals(other.sql))
			return false;
		if (typesColsSelect == null) {
			if (other.typesColsSelect != null)
				return false;
		} else if (!typesColsSelect.equals(other.typesColsSelect))
			return false;
		return true;
	}
	
	
	
}
